//
//  ZMSearchVC.swift
//  zhonglibang
//
//  Created by 刘亿前 on 2021/1/15.
//

import UIKit
import LTScrollView

class ZMSearchVC: ZMViewController {

    let nav = NavigationBarView()
    
    private lazy var layout: LTLayout = {
        let layout = LTLayout()
        layout.titleViewBgColor = UIColor.white
        layout.titleSelectColor = Style.mainColor
        layout.bottomLineColor = Style.mainColor
        layout.lrMargin = 16
        layout.titleMargin = 36
        layout.titleColor = "#333333".zm.hex()
        layout.isNeedScale = false
        layout.titleFont = UIFont.zm.fontMedium(size: 16)
        layout.showsHorizontalScrollIndicator = false
        if !homeSearch {
            layout.sliderHeight = 0
            layout.bottomLineHeight = 0
            layout.isScrollEnabled = false
        }
        return layout
    }()
        
    private lazy var controllers: [ZMSearchListVC] = {
        var arr = [ZMSearchListVC]()
        titles.forEach { val in
            let vc = ZMSearchListVC()
            vc.type = val["id"]!
            vc.homeSearch = homeSearch
            arr.append(vc)
            vc.historyClick = { [unowned self] val in
                self.searchTxt.text = val
            }
        }
        return arr
    }()
    
    private lazy var advancedManager: LTAdvancedManager = {
        let manager = LTAdvancedManager(frame: CGRect(x: 0, y: nav.zm.height, width: SCREEN_W, height: SCREEN_H - nav.zm.height), viewControllers: controllers, titles: titles.map{return $0["name"]!}, currentViewController: self, layout: layout, headerViewHandle: {
            return UIView()
        })
        manager.backgroundColor = .clear
        manager.advancedDidSelectIndexHandle = { [unowned self] idx in
            self.advancedIndex = idx
        }
        return manager
    }()
    
    private var titles = [["id":"tb","name":"淘宝"],["id":"jd","name":"京东"],["id":"pdd","name":"拼多多"]]
    
    // searchView
    let searchView = SearchView()
    
    var searchTxt: UITextField {
        get {
            return searchView.searchText
        }
    }
    /// 搜索历史
    var history = [String]()
    var advancedIndex = 0
    var homeSearch = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        hbd_barHidden = true
        view.addSubview(nav)
        bindAction()
        
        if !homeSearch {
            titles = [["id":"tb","name":""],["id":"jd","name":""],["id":"pdd","name":""]]
        }
        
        nav.snp.makeConstraints { make in
            make.top.right.left.equalToSuperview()
        }
        nav.backButton.isHidden = true
     
        nav.setRightButton { [weak self] btn in
//            guard let self = self else {return}
            btn.setTitle("取消", for: .normal)
            btn.setTitleColor(UIColor.zm.color333333(), for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            btn.snp.makeConstraints { make in
                make.width.equalTo(35)
            }
        } _: { [unowned self] in
            self.navigationController?.popViewController(animated: true)
        }

        searchView.layer.cornerRadius = 8
        searchView.layer.masksToBounds = true
        nav.contentView.addSubview(searchView)
        searchView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.height.equalTo(36)
            make.right.equalTo(nav.rightButton.snp.left).offset(-25)
        }
        nav.layoutIfNeeded()
        view.addSubview(advancedManager)
        advancedManager.scrollToIndex(index: advancedIndex)
    }
    
    func bindAction() {
        searchTxt.rx.text.orEmpty.asObservable().subscribe(onNext: { [unowned self] val in
            self.controllers.forEach { vc in
                vc.searchKey = val
            }
        }).disposed(by: disposeBag)
        
        searchTxt.rx.controlEvent([.editingDidEndOnExit]).asObservable().subscribe(onNext:  { [unowned self] in
            guard let key = self.searchTxt.text, key.count != 0 else {
                ZMToast.showToast(title: "请输入关键字")
                return
            }
            self.controllers[self.advancedIndex].requestList()
        }).disposed(by: disposeBag)
    }
}

